#include "logoscene.h"
#include <vector>

LogoScene::LogoScene(const std::string &filename,bool sound, const sf::Color &backgroundColor, float fadeTime,float logoTime){

	logoFilename=filename;

	this->backgroundColor=backgroundColor;
	this->fadeTime=fadeTime;
	this->logoTime=logoTime;
	elapsedTime=0.0;

	if(sound)
        {
                buffSound.LoadFromFile("sound/logo_sound.ogg");
                sndLogo.SetBuffer(buffSound);
                sndLogo.Play();
	}
	
}



void LogoScene::Init(){

	const sf::Image &imgLogo=theTextureManager.GetTexture(logoFilename);
	sprLogo.SetImage(imgLogo);

	const sf::View &vista=theEngine.GetRenderWindow().GetView();

	sprLogo.SetPosition(vista.GetHalfSize().x-imgLogo.GetWidth()/2.0, vista.GetHalfSize().y-imgLogo.GetHeight()/2.0);


}




void LogoScene::CleanUp(){
	theTextureManager.UnloadTexture(logoFilename);
}




void LogoScene::Render(sf::RenderWindow &w){
	w.Clear(backgroundColor);
	w.Draw(sprLogo);
}



void LogoScene::Update(const float &dt){
	elapsedTime+=dt;
	
	float alpha;
	if(elapsedTime<fadeTime)
	{
		alpha=elapsedTime/fadeTime;
	}
	else if(elapsedTime>=fadeTime && elapsedTime < fadeTime+logoTime)
	{
		alpha=1.0;
	}
	else if(elapsedTime<= fadeTime*2+logoTime)
	{	
		alpha=1.0 - ((elapsedTime-logoTime-fadeTime)/fadeTime);
	}
	else
	{
		alpha=0.0;
		EndScene();
	}		
	
	sprLogo.SetColor(sf::Color(255, 255, 255, alpha*255));
}


void LogoScene::ProcessEvent(const sf::Event &e){
	
	if((e.Type == sf::Event::KeyPressed || e.Type == sf::Event::MouseButtonPressed || e.Type==sf::Event::JoyButtonPressed) && elapsedTime<fadeTime + logoTime){
		elapsedTime = fadeTime + logoTime;
	}

}
